package com.dunqi.gpm.cangxi.home.adapter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.home.activity.InstructionsActivity;
import com.dunqi.gpm.cangxi.home.bean.tree.Data;
import com.dunqi.gpm.cangxi.home.eventbus.TreeViewDelete;
import com.dunqi.gpm.cangxi.home.widget.DashedLineView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;
import com.unnamed.b.atv.model.TreeNode;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by 空 on 2017/12/19 0019.
 */

public class TreeHolder extends TreeNode.BaseNodeViewHolder<Data> {

    private final Long mainObjectId;
    private final Long instructionType;
    private LayoutInflater inflater;
    int[] resId = {R.mipmap.icon_instructions, R.mipmap.icon_instructions, R.mipmap.d, R.mipmap.f};
    int px;

    public TreeHolder(Context context, Long mainObjectId, Long instructionType) {
        super(context);
        this.mainObjectId = mainObjectId;
        this.instructionType = instructionType;
        inflater = LayoutInflater.from(context);
        px = CommonUtil.dip2px(10);

    }

    @Override
    public View createNodeView(TreeNode node, final Data value) {

        final View view = inflater.inflate(R.layout.item_tree, null);
        final TextView replyTv = view.findViewById(R.id.reply_tv);
        final TextView deleteTv = view.findViewById(R.id.delete_tv);
        final FrameLayout container = view.findViewById(R.id.container);
        View line = view.findViewById(R.id.line);
        View dashedLine1 = view.findViewById(R.id.dashedLine1);
        View dashedLine2 = view.findViewById(R.id.dashedLine2);
        final View dashedLine3 = view.findViewById(R.id.dashedLine3);

        final ImageView cb = view.findViewById(R.id.cb);

        int level = node.getLevel();
        level--;

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) cb.getLayoutParams();
        params.setMargins(2 * px * level + px, 0, 0, 0);

        FrameLayout.LayoutParams paramsLine = (FrameLayout.LayoutParams) line.getLayoutParams();
        paramsLine.setMargins(2 * level * px, px, 0, 0);

        FrameLayout.LayoutParams dashedLine1Params = (FrameLayout.LayoutParams) dashedLine1.getLayoutParams();
        dashedLine1Params.setMargins(2 * level * px, 0, 0, 0);

        FrameLayout.LayoutParams dashedLine2Params = (FrameLayout.LayoutParams) dashedLine2.getLayoutParams();
        dashedLine2Params.setMargins(2 * level * px, 0, 0, 0);

        FrameLayout.LayoutParams dashedLine3Params = (FrameLayout.LayoutParams) dashedLine3.getLayoutParams();
        dashedLine3Params.setMargins(2 * (level + 1) * px, 0, 0, 0);

        final List<TreeNode> children = node.getChildren();
        if (children == null || children.isEmpty()) {
            dashedLine3.setVisibility(View.GONE);
        } else {
            dashedLine3.setVisibility(View.VISIBLE);
        }
        List<TreeNode> parentChildren = node.getParent().getChildren();
        if (parentChildren.indexOf(node) == parentChildren.size() - 1) {
            dashedLine2.setVisibility(View.GONE);
        } else {
            dashedLine2.setVisibility(View.VISIBLE);
        }

        if (level == 0) {
            dashedLine1.setVisibility(View.GONE);
            dashedLine2.setVisibility(View.GONE);
        } else {
            dashedLine1.setVisibility(View.VISIBLE);
//            dashedLine2.setVisibility(View.VISIBLE);
        }

        initDashedLine(node, container, node.getLevel());


        int msgType = value.getMsgType();
        ImageView icon_iv = view.findViewById(R.id.icon_iv);
        icon_iv.setImageResource(resId[msgType]);

        final TextView tv = view.findViewById(R.id.text);
        tv.setText(value.getInstructionText());

        Long instructorId = value.getInstructorId();
        int id = UserUtil.getUserId();
        if (id == instructorId) {
            deleteTv.setVisibility(View.VISIBLE);
        } else {
            deleteTv.setVisibility(View.INVISIBLE);
        }

        deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!v.isShown()) {
                    return;
                }
                Long projectInstructionId = value.getProjectInstructionId();

                final ProgressDialog dialog = ProgressDialog.show(v.getContext(), "提示", "正在删除...");
                ApiFactory.getInstance()
                        .projectInstructionDelete(projectInstructionId)
                        .compose(RxSchedulers.<com.dunqi.gpm.cangxi.common.base.Data>io_main())
                        .subscribe(new ComObserver() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onSuccess(Object o) {
                                CommonUtil.showSingleToast("删除成功");
                                EventBus.getDefault().post(new TreeViewDelete());
                            }

                            @Override
                            public void onFinal() {
                                dialog.dismiss();
                            }
                        });


            }
        });

        tv.post(new Runnable() {
            @Override
            public void run() {
                int width = tv.getWidth();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv.getLayoutParams();
                params.weight = 0;
                tv.setMaxWidth(width);
                tv.postInvalidate();
            }
        });


        TextView titlTv = view.findViewById(R.id.title_tv);
        titlTv.setText(value.getUserName());

        TextView timeTv = view.findViewById(R.id.time_tv);
        timeTv.setText(value.getInstructTime());


        if (!value.isChecked()) {
            cb.setImageResource(R.mipmap.j_n);
        } else {
            cb.setImageResource(R.mipmap.j_h);
        }

        node.setClickListener(new TreeNode.TreeNodeClickListener() {
            @Override
            public void onClick(TreeNode node, Object value1) {
                value.setChecked(node.isExpanded());
                if (node.isExpanded()) {
                    cb.setImageResource(R.mipmap.j_n);
                    dashedLine3.setVisibility(View.GONE);
                } else {
                    cb.setImageResource(R.mipmap.j_h);
                    if (children == null || children.isEmpty()) {
                        dashedLine3.setVisibility(View.GONE);
                    } else {
                        dashedLine3.setVisibility(View.VISIBLE);
                    }
                }
            }
        });


        replyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回复mainObjectId replyProInstructionId
                InstructionsActivity.start((Activity) context, mainObjectId, instructionType, 3, value.getProjectInstructionId());
            }
        });

        return view;
    }

    private void initDashedLine(final TreeNode node, final FrameLayout container, int lastLevel) {
        TreeNode parent = node.getParent();
        if (parent == null) {
            return;
        }
        final int level = parent.getLevel();

        if (lastLevel - level < 2) {
            initDashedLine(parent, container, lastLevel);
            return;
        }


        List<TreeNode> children = parent.getChildren();

        if (children.indexOf(node) == children.size() - 1) {

        } else {
            if (level == 0) {
                return;
            }
            container.post(new Runnable() {
                @Override
                public void run() {
                    DashedLineView view = new DashedLineView(container.getContext());
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(CommonUtil.dip2px(3),
                            container.getHeight());
                    view.setLayoutParams(params);
                    params.setMargins(2 * px * level, 0, 0, 0);
                    container.addView(view, 0);

                }
            });
        }


        initDashedLine(parent, container, lastLevel);
    }


}
